<script setup lang="ts">
import type { ConsultEnum } from '@/enums';
import ConsultItem from '../components/ConsultItem.vue'
import { ref } from 'vue';
import type { ConsultOrderItem } from '@/types/consult';
import { ConsultOrderList } from '@/apis/consult';
defineOptions({
  name: 'ConsultList'
})
const props = defineProps<{
  type: ConsultEnum
}>()

//定义查询参数数据
const consultParams = ref({
  current: 1,
  pageSize: 10,
  type: props.type
})
//定义响应式数据
const list = ref<ConsultOrderItem[]>([])
const loading = ref(false)
const finished = ref(false)
//下拉加载
const onLoad = async () => {
  const res = await ConsultOrderList(consultParams.value)
  list.value.push(...res.data.rows)
  // 加载状态结束
  loading.value = false
  consultParams.value.current++
  if (consultParams.value.current > res.data.pageTotal) {
    finished.value = true
  }
}

//删除订单
const ondelete = (id: string) => {
  list.value = list.value.filter(item => item.id !== id)
  //删除完成重新渲染数据
  if (list.value.length === 0) onLoad()
}
</script>

<template>
  <div class="consult-list">
    <van-list v-model:loading="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <consult-item v-for="item in list" :key="item.id" :item="item" @on-delete="ondelete(item.id)" />
    </van-list>

  </div>
</template>

<style lang="scss" scoped>
.consult-list {
  padding: 10px 15px;
}
</style>
